home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / SourceCode / MiniExamples / Grabber / Grabber.m < prev    next >
Encoding:
Text File  |  1992-12-19  |  1.9 KB  |  78 lines

  1. /* Grabber.m
  2. *  Purpose:  This class grabs the contents of the screen under the 
  3. *  grabberWindow and uses
  4. *  it to create a TIFF file.
  5. *
  6. *  You may freely copy, distribute, and reuse the code in this example.
  7. *  NeXT disclaims any warranty of any kind, expressed or  implied, as to 
  8. *  its fitness for any particular use.
  9. */
  10.  
  11. #import "Grabber.h"
  12. #import <appkit/NXBitmapImageRep.h>
  13. #import <appkit/tiff.h>
  14. #import <appkit/Window.h>
  15. #import <appkit/View.h>
  16. #import <dpsclient/wraps.h>
  17.  
  18. @implementation Grabber
  19.  
  20. - setGrabberWindow:anObject
  21. {
  22.     grabberWindow = anObject;
  23.  
  24.     /* Set the window level... */
  25.     PSsetwindowlevel (100, [grabberWindow windowNum]);
  26.  
  27.     /* Without the next line the level doesn't seem to be set; */
  28.     /* a window server bug. */
  29.     [grabberWindow orderFront:nil];
  30.     [grabberWindow display];
  31.     return self;
  32. }
  33.  
  34. - grab:sender
  35. {
  36.     NXRect grabRect;
  37.     id bitmap;
  38.  
  39.     /* Grab the screen bits into the window. */
  40.     PSsetautofill (NO, [grabberWindow windowNum]);
  41.     [grabberWindow orderOut:nil];
  42.     [grabberWindow orderFront:nil];
  43.  
  44.     /* Read the bits from the window */
  45.     [[grabberWindow contentView] lockFocus];
  46.     [[grabberWindow contentView] getBounds:&grabRect];
  47.     bitmap = [[NXBitmapImageRep alloc] initData:NULL fromRect:&grabRect];
  48.     [[grabberWindow contentView] unlockFocus];
  49.  
  50.     PSsetautofill (YES, [grabberWindow windowNum]);
  51.     [grabberWindow display];
  52.  
  53.     if (bitmap) {
  54.     NXStream *s = NXOpenMemory (NULL, 0, NX_READWRITE);
  55.     printf ("spp %d bps %d w %d h %d\n",
  56.         [bitmap samplesPerPixel],
  57.         [bitmap bitsPerSample],
  58.         [bitmap pixelsWide],
  59.         [bitmap pixelsHigh]);
  60.     if (s) {
  61.         [bitmap writeTIFF:s usingCompression:NX_TIFF_COMPRESSION_LZW];
  62.         NXFlush (s);
  63.         if (NXSaveToFile (s, "/tmp/screenshot.tiff")) {
  64.         perror ("/tmp/screenshot.tiff");
  65.         } else {
  66.         printf ("Image written to /tmp/screenshot.tiff\n");
  67.         }
  68.         NXCloseMemory (s, NX_FREEBUFFER);
  69.     }
  70.     [bitmap free];
  71.     }
  72.  
  73.     return self;
  74. }
  75.  
  76.  
  77. @end
  78.